package com.gugee.ins.common.dal.mapper.brand;

import com.gugee.ins.common.model.mysql.blogger.InsBloggerMain;
import com.gugee.ins.common.model.mysql.brand.InsRelBrandMedia;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * InsBrandMapper
 * @author huangxin
 */
public interface InsBrandExtMapper extends InsBrandMapper {
    /**
     * 根据bid更新分类
     * @param bid
     * @param updateTime
     */
    void updateCategory(@Param("bid")int bid,@Param("updateTime")long updateTime);

    /**
     * 获取品牌下的所有博主
     * @param bid
     * @return
     */
    @Select("select b.* from ins_rel_brand_blogger rb inner join ins_blogger_main b on b.uid = rb.uid where rb.bid = #{bid}")
    List<InsBloggerMain> findBrandBloggerByBid(@Param("bid") int bid);


    /**
     * 获取@品牌下博主的帖子
     * @param usernames
     * @param bid
     * @param beginTime
     * @param endTime
     * @return
     */
    @Select({"<script>",
            "SELECT m.shortcode,m.owner_uid,m.is_video,m.product_type,m.taken_time,m.score,#{bid} bid,now() update_time,m.is_ad,m.is_sponsored,m.product_tag_type," +
                    "0 is_brand_account,m.id media_id,m.like_count,m.comment_count,m.er,m.video_view_count,m.video_play_count " +
                    "FROM ins_rel_media_caption_tagger ct inner join ins_media m on ct.shortcode = m.shortcode " +
                    "WHERE ct.caption_tagger_name IN ",
            "<foreach item='username' index='index' collection='usernames' open='(' separator=',' close=') '>",
            "#{username}",
            "</foreach>",
            "AND ct.taken_time BETWEEN #{beginTime} AND #{endTime}",
            "</script>"})
    List<InsRelBrandMedia> findMediaForCaptionTagger(List<String> usernames,int bid,long beginTime,long endTime);

    /**
     * 获取标记品牌下博主的帖子
     * @param uIds
     * @param bid
     * @param beginTime
     * @param endTime
     * @return
     */
    @Select({"<script>",
            "SELECT m.shortcode,m.owner_uid,m.is_video,m.product_type,m.taken_time,m.score,#{bid} bid,now() update_time,m.is_ad,m.is_sponsored,m.product_tag_type," +
                    "0 is_brand_account,m.id media_id,m.like_count,m.comment_count,m.er,m.video_view_count,m.video_play_count " +
                    "FROM ins_rel_media_tagger_user tu left join ins_media m on tu.shortcode = m.shortcode " +
                    "WHERE tu.tagger_uid IN ",
            "<foreach item='uid' index='index' collection='uIds' open='(' separator=',' close=') '>",
            "#{uid}",
            "</foreach>",
            "AND tu.taken_time BETWEEN #{beginTime} AND #{endTime}",
            "</script>"})
    List<InsRelBrandMedia> findMediaForTaggerUser(List<Long> uIds,int bid,long beginTime,long endTime);


    /**
     * 获取品牌下博主赞助的帖子
     * @param uIds
     * @param bid
     * @param beginTime
     * @param endTime
     * @return
     */
    @Select({"<script>",
            "SELECT m.shortcode,m.owner_uid,m.is_video,m.product_type,m.taken_time,m.score,#{bid} bid,now() update_time,m.is_ad,m.is_sponsored,m.product_tag_type," +
                    "0 is_brand_account,m.id media_id,m.like_count,m.comment_count,m.er,m.video_view_count,m.video_play_count " +
                    "FROM ins_rel_media_sponsor_user su left join ins_media m on su.shortcode = m.shortcode " +
                    "WHERE su.sponsor_uid IN ",
            "<foreach item='uid' index='index' collection='uIds' open='(' separator=',' close=') '>",
            "#{uid}",
            "</foreach>",
            "AND su.taken_time BETWEEN #{beginTime} AND #{endTime}",
            "</script>"})
    List<InsRelBrandMedia> findMediaForSponsorUser(List<Long> uIds,int bid,long beginTime,long endTime);


    /**
     * 获取品牌下的博主下的商品被标记的帖子
     * @param uIds
     * @param bid
     * @param beginTime
     * @param endTime
     * @return
     */
    @Select({"<script>",
            "SELECT m.shortcode,m.owner_uid,m.is_video,m.product_type,m.taken_time,m.score,#{bid} bid,now() update_time,m.is_ad,m.is_sponsored,m.product_tag_type," +
                    "0 is_brand_account,m.id media_id,m.like_count,m.comment_count,m.er,m.video_view_count,m.video_play_count " +
                    "FROM (SELECT product_id FROM ins_product WHERE merchant_uid IN",
            "<foreach item='uid' index='index' collection='uIds' open='(' separator=',' close=') '>",
            "#{uid}",
            "</foreach>",
            ") ip INNER JOIN ins_rel_media_product irmp ON ip.product_id = irmp.product_id " +
                    " inner join ins_media m on irmp.shortcode = m.shortcode " +
            "AND irmp.taken_time BETWEEN #{beginTime} AND #{endTime}",
            "</script>"})
    List<InsRelBrandMedia> findMediaForProduct(List<Long> uIds,int bid,long beginTime,long endTime);


    /**
     * 获取品牌下的博主下的帖子
     * @param uIds
     * @param bid
     * @param beginTime
     * @param endTime
     * @return
     */
    @Select({"<script>",
            "SELECT m.shortcode,m.owner_uid,m.is_video,m.product_type,m.taken_time,m.score,#{bid} bid,now() update_time,m.is_ad,m.is_sponsored,m.product_tag_type," +
                    "1 is_brand_account,m.id media_id,m.like_count,m.comment_count,m.er,m.video_view_count,m.video_play_count " +
                    "FROM ins_media m " +
                    "WHERE m.owner_uid IN ",
            "<foreach item='uid' index='index' collection='uIds' open='(' separator=',' close=') '>",
            "#{uid}",
            "</foreach>",
            "AND m.taken_time BETWEEN #{beginTime} AND #{endTime}",
            "</script>"})
    List<InsRelBrandMedia> findMediaForBlogger(List<Long> uIds,int bid,long beginTime,long endTime);
}